package com.luuyi.actionchain.action;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.TextView;

import com.luuyi.actionchain.R;

public class DialogAction extends AbsAction {

    public static final String DEFAULT_CONTENT = "Default Content";

    private CommonDialog mDialog;
    private String dialogContent = DEFAULT_CONTENT;

    public DialogAction(Context context) {
        mDialog = new CommonDialog(context);
        setIsSyncAction(false);
    }

    public DialogAction setContent(String content) {
        dialogContent = content;
        return this;
    }

    public String getContent() {
        return dialogContent;
    }

    @Override
    public void handle() {
        mDialog.setOnDismissListener(dialog -> doNext());
        mDialog.setContent(TextUtils.isEmpty(dialogContent) ? DEFAULT_CONTENT : dialogContent);
        mDialog.show();
    }

    public class CommonDialog extends Dialog {

        private TextView tvContent;

        public CommonDialog(@NonNull Context context) {
            this(context, 0);
        }

        public CommonDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);

            init();
        }

        private void init() {
            setContentView(R.layout.layout_common_dialog);
            tvContent = findViewById(R.id.tv_content);
            findViewById(R.id.tv_btn).setOnClickListener(v -> cancel());
        }

        private void setContent(String content) {
            tvContent.setText(content);
        }
    }
}
